# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import json

import pymongo
# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import redis


class MyOneScrapyPipeline:
    def open_spider(self, spider):
        self.client = pymongo.MongoClient("mongodb://localhost:27017/")
        self.db = self.client.get_database("my_one_scrapy")
        self.collection = self.db.get_collection("quotes")


    def process_item(self, item, spider):
        if spider.name == "quotes_allpage":
            self.collection.insert_one(item)
        return item

    def close_spider(self, spider):
        self.client.close()

class ProducerPipeline:
    def open_spider(self, spider):
        self.client = redis.Redis()
        self.client.delete("tasks")

    def process_item(self, item, spider):
        self.client.lpush("tasks", json.dumps(item, ensure_ascii=False))
        return item
    def close_spider(self, spider):
        self.client.close()